﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using LibKetoan;

namespace Ketoan
{
	/// <summary>
	/// Summary description for frmNhapmlns.
	/// </summary>
	public class frmDmmlns : System.Windows.Forms.Form
	{
		#region khai bao bien toan cuc
		private string s_cc="",s_ttk="",s_stk="",s_mtm="";
		private string s_n="",s_tn="";
		private int i_sudung=0,i_huy=0,i_cd=0,i_nhmuc=0,i_sp=0;
		
		private DataTable dtnh=new DataTable();
		private DataTable dttn=new DataTable();
		private DataTable dtmuc=new DataTable();
		private DataTable dttm=new DataTable();
		private DataTable dttmct=new DataTable();
		private DataTable dtnhmc=new DataTable();
		
		private DataSet dsnhom = new DataSet();
		private DataSet dsnhmuc = new DataSet();
		private DataSet dstn = new DataSet();
		private DataSet dsmuc = new DataSet();
		private DataSet dstm = new DataSet();
		private DataSet dstmct = new DataSet();

		private DataSet ds = new DataSet();

		private Brush disabledTextBrush;
		private Brush currentRowBackBrush;
		private Font currentRowFont;
		private bool b_tn=true,b_muc=true,b_tm=true,b_tmct=true,b_them=true;
		private long l_id=0;

		private AccessData a;
		private int i_userid=0;
		private string s_mmyy="";
		#endregion dong khai bao bien toan cuc
		#region controls
		private System.Windows.Forms.TextBox tim;
		private System.Windows.Forms.DataGrid dataGrid1;
		private System.Windows.Forms.Button butHuy;
		private System.Windows.Forms.Button butBoqua;
		private System.Windows.Forms.Button butLuu;
		private System.Windows.Forms.Button butSua;
		private System.Windows.Forms.Button butMoi;
		private System.Windows.Forms.Button butKetthuc;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.ComboBox cboLoaicapnhat;
		private LibList.List listnhom;
		private System.Windows.Forms.TextBox txttennhom;
		private System.Windows.Forms.TextBox txtnhom;
		private System.Windows.Forms.TextBox txttieunhom;
		private LibList.List listtieunhom;
		private System.Windows.Forms.TextBox txttentieunhom;
		private System.Windows.Forms.TextBox txtmuc;
		private LibList.List listmuc;
		private System.Windows.Forms.TextBox txttenmuc;
		private System.Windows.Forms.TextBox txttm;
		private LibList.List listtm;
		private System.Windows.Forms.TextBox txttentm;
		private System.Windows.Forms.TextBox txttmct;
		private System.Windows.Forms.TextBox txttentmct;
		private System.Windows.Forms.TextBox txtnhmc;
		private LibList.List listnhmc;
		private System.Windows.Forms.TextBox txttennhmc;
		#endregion controls
		private LibList.List listtmct;
		private System.Windows.Forms.CheckBox chkSudung;
		private System.Windows.Forms.Button btn_in;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmDmmlns(AccessData da,int iUserid,string smmyy)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			a=da;
			i_userid=iUserid;
			s_mmyy=smmyy;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmDmmlns));
			this.tim = new System.Windows.Forms.TextBox();
			this.dataGrid1 = new System.Windows.Forms.DataGrid();
			this.butHuy = new System.Windows.Forms.Button();
			this.butBoqua = new System.Windows.Forms.Button();
			this.butLuu = new System.Windows.Forms.Button();
			this.butSua = new System.Windows.Forms.Button();
			this.butMoi = new System.Windows.Forms.Button();
			this.butKetthuc = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.cboLoaicapnhat = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.label6 = new System.Windows.Forms.Label();
			this.label7 = new System.Windows.Forms.Label();
			this.listnhom = new LibList.List();
			this.txttennhom = new System.Windows.Forms.TextBox();
			this.txtnhom = new System.Windows.Forms.TextBox();
			this.txttieunhom = new System.Windows.Forms.TextBox();
			this.listtieunhom = new LibList.List();
			this.txttentieunhom = new System.Windows.Forms.TextBox();
			this.txtmuc = new System.Windows.Forms.TextBox();
			this.listmuc = new LibList.List();
			this.txttenmuc = new System.Windows.Forms.TextBox();
			this.txttm = new System.Windows.Forms.TextBox();
			this.listtm = new LibList.List();
			this.txttentm = new System.Windows.Forms.TextBox();
			this.txttmct = new System.Windows.Forms.TextBox();
			this.listtmct = new LibList.List();
			this.txttentmct = new System.Windows.Forms.TextBox();
			this.txtnhmc = new System.Windows.Forms.TextBox();
			this.listnhmc = new LibList.List();
			this.txttennhmc = new System.Windows.Forms.TextBox();
			this.chkSudung = new System.Windows.Forms.CheckBox();
			this.btn_in = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// tim
			// 
			this.tim.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.tim.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.tim.Location = new System.Drawing.Point(8, 0);
			this.tim.Name = "tim";
			this.tim.Size = new System.Drawing.Size(776, 21);
			this.tim.TabIndex = 41;
			this.tim.Text = "Tìm kiếm";
			this.tim.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
			this.tim.TextChanged += new System.EventHandler(this.tim_TextChanged);
			this.tim.Enter += new System.EventHandler(this.tim_Enter);
			// 
			// dataGrid1
			// 
			this.dataGrid1.AlternatingBackColor = System.Drawing.Color.Lavender;
			this.dataGrid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.dataGrid1.BackColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid1.BackgroundColor = System.Drawing.SystemColors.Control;
			this.dataGrid1.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.dataGrid1.CaptionBackColor = System.Drawing.SystemColors.Control;
			this.dataGrid1.CaptionFont = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid1.CaptionForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.DataMember = "";
			this.dataGrid1.FlatMode = true;
			this.dataGrid1.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.dataGrid1.ForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.GridLineColor = System.Drawing.Color.Gainsboro;
			this.dataGrid1.GridLineStyle = System.Windows.Forms.DataGridLineStyle.None;
			this.dataGrid1.HeaderFont = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Bold);
			this.dataGrid1.HeaderForeColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid1.LinkColor = System.Drawing.Color.Teal;
			this.dataGrid1.Location = new System.Drawing.Point(8, 24);
			this.dataGrid1.Name = "dataGrid1";
			this.dataGrid1.ParentRowsBackColor = System.Drawing.Color.Gainsboro;
			this.dataGrid1.ParentRowsForeColor = System.Drawing.Color.MidnightBlue;
			this.dataGrid1.ReadOnly = true;
			this.dataGrid1.RowHeaderWidth = 10;
			this.dataGrid1.SelectionBackColor = System.Drawing.Color.CadetBlue;
			this.dataGrid1.SelectionForeColor = System.Drawing.Color.WhiteSmoke;
			this.dataGrid1.Size = new System.Drawing.Size(776, 312);
			this.dataGrid1.TabIndex = 42;
			this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged);
			// 
			// butHuy
			// 
			this.butHuy.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.butHuy.Image = ((System.Drawing.Bitmap)(resources.GetObject("butHuy.Image")));
			this.butHuy.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butHuy.Location = new System.Drawing.Point(488, 536);
			this.butHuy.Name = "butHuy";
			this.butHuy.Size = new System.Drawing.Size(60, 25);
			this.butHuy.TabIndex = 48;
			this.butHuy.Text = "    &Hủy";
			this.butHuy.Click += new System.EventHandler(this.butHuy_Click);
			// 
			// butBoqua
			// 
			this.butBoqua.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.butBoqua.Enabled = false;
			this.butBoqua.Image = ((System.Drawing.Bitmap)(resources.GetObject("butBoqua.Image")));
			this.butBoqua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butBoqua.Location = new System.Drawing.Point(424, 536);
			this.butBoqua.Name = "butBoqua";
			this.butBoqua.Size = new System.Drawing.Size(60, 25);
			this.butBoqua.TabIndex = 44;
			this.butBoqua.Text = "&Bỏ qua";
			this.butBoqua.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butBoqua.Click += new System.EventHandler(this.butBoqua_Click);
			// 
			// butLuu
			// 
			this.butLuu.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.butLuu.Enabled = false;
			this.butLuu.Image = ((System.Drawing.Bitmap)(resources.GetObject("butLuu.Image")));
			this.butLuu.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butLuu.Location = new System.Drawing.Point(360, 536);
			this.butLuu.Name = "butLuu";
			this.butLuu.Size = new System.Drawing.Size(60, 25);
			this.butLuu.TabIndex = 43;
			this.butLuu.Text = "     &Lưu";
			this.butLuu.Click += new System.EventHandler(this.butLuu_Click);
			// 
			// butSua
			// 
			this.butSua.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.butSua.Image = ((System.Drawing.Bitmap)(resources.GetObject("butSua.Image")));
			this.butSua.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butSua.Location = new System.Drawing.Point(296, 536);
			this.butSua.Name = "butSua";
			this.butSua.Size = new System.Drawing.Size(60, 25);
			this.butSua.TabIndex = 46;
			this.butSua.Text = "    &Sửa";
			this.butSua.Click += new System.EventHandler(this.butSua_Click);
			// 
			// butMoi
			// 
			this.butMoi.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.butMoi.Image = ((System.Drawing.Bitmap)(resources.GetObject("butMoi.Image")));
			this.butMoi.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butMoi.Location = new System.Drawing.Point(224, 536);
			this.butMoi.Name = "butMoi";
			this.butMoi.Size = new System.Drawing.Size(64, 25);
			this.butMoi.TabIndex = 45;
			this.butMoi.Text = "     &Thêm";
			this.butMoi.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butMoi.Click += new System.EventHandler(this.butMoi_Click);
			// 
			// butKetthuc
			// 
			this.butKetthuc.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.butKetthuc.Image = ((System.Drawing.Bitmap)(resources.GetObject("butKetthuc.Image")));
			this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.butKetthuc.Location = new System.Drawing.Point(632, 536);
			this.butKetthuc.Name = "butKetthuc";
			this.butKetthuc.Size = new System.Drawing.Size(70, 25);
			this.butKetthuc.TabIndex = 49;
			this.butKetthuc.Text = "&Kết thúc";
			this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
			this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
			// 
			// label1
			// 
			this.label1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label1.Location = new System.Drawing.Point(8, 336);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(96, 23);
			this.label1.TabIndex = 50;
			this.label1.Text = "Loại cập nhật:";
			// 
			// cboLoaicapnhat
			// 
			this.cboLoaicapnhat.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.cboLoaicapnhat.Enabled = false;
			this.cboLoaicapnhat.Items.AddRange(new object[] {
																"Tiểu nhóm",
																"Mục",
																"Tiểu mục",
																"Tiểu mục chi tiết"});
			this.cboLoaicapnhat.Location = new System.Drawing.Point(104, 336);
			this.cboLoaicapnhat.Name = "cboLoaicapnhat";
			this.cboLoaicapnhat.Size = new System.Drawing.Size(680, 21);
			this.cboLoaicapnhat.TabIndex = 51;
			this.cboLoaicapnhat.SelectedIndexChanged += new System.EventHandler(this.cboLoaicapnhat_SelectedIndexChanged);
			// 
			// label2
			// 
			this.label2.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label2.Location = new System.Drawing.Point(8, 360);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 23);
			this.label2.TabIndex = 52;
			this.label2.Text = "Nhóm:";
			// 
			// label3
			// 
			this.label3.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label3.Location = new System.Drawing.Point(8, 384);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(96, 23);
			this.label3.TabIndex = 53;
			this.label3.Text = "Tiểu nhóm:";
			// 
			// label4
			// 
			this.label4.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label4.Location = new System.Drawing.Point(8, 408);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(96, 23);
			this.label4.TabIndex = 54;
			this.label4.Text = "Mục:";
			// 
			// label5
			// 
			this.label5.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label5.Location = new System.Drawing.Point(8, 432);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(96, 23);
			this.label5.TabIndex = 55;
			this.label5.Text = "Tiểu mục:";
			// 
			// label6
			// 
			this.label6.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label6.Location = new System.Drawing.Point(8, 456);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(96, 23);
			this.label6.TabIndex = 56;
			this.label6.Text = "Tiểu mục chi tiết:";
			// 
			// label7
			// 
			this.label7.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.label7.Location = new System.Drawing.Point(8, 480);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(96, 23);
			this.label7.TabIndex = 57;
			this.label7.Text = "Nhóm mục chi:";
			// 
			// listnhom
			// 
			this.listnhom.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.listnhom.BackColor = System.Drawing.SystemColors.Info;
			this.listnhom.ColumnCount = 0;
			this.listnhom.Location = new System.Drawing.Point(256, 360);
			this.listnhom.MatchBufferTimeOut = 1000;
			this.listnhom.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
			this.listnhom.Name = "listnhom";
			this.listnhom.Size = new System.Drawing.Size(480, 17);
			this.listnhom.TabIndex = 155;
			this.listnhom.TextIndex = -1;
			this.listnhom.TextMember = null;
			this.listnhom.ValueIndex = -1;
			this.listnhom.Visible = false;
			// 
			// txttennhom
			// 
			this.txttennhom.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.txttennhom.Enabled = false;
			this.txttennhom.Location = new System.Drawing.Point(240, 360);
			this.txttennhom.Name = "txttennhom";
			this.txttennhom.Size = new System.Drawing.Size(544, 20);
			this.txttennhom.TabIndex = 154;
			this.txttennhom.Text = "";
			// 
			// txtnhom
			// 
			this.txtnhom.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.txtnhom.Enabled = false;
			this.txtnhom.Location = new System.Drawing.Point(104, 360);
			this.txtnhom.Name = "txtnhom";
			this.txtnhom.Size = new System.Drawing.Size(136, 20);
			this.txtnhom.TabIndex = 156;
			this.txtnhom.Text = "";
			this.txtnhom.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtnhom_KeyDown);
			this.txtnhom.Validated += new System.EventHandler(this.txtnhom_Validated);
			this.txtnhom.TextChanged += new System.EventHandler(this.txtnhom_TextChanged);
			// 
			// txttieunhom
			// 
			this.txttieunhom.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.txttieunhom.Enabled = false;
			this.txttieunhom.Location = new System.Drawing.Point(104, 384);
			this.txttieunhom.Name = "txttieunhom";
			this.txttieunhom.Size = new System.Drawing.Size(136, 20);
			this.txttieunhom.TabIndex = 159;
			this.txttieunhom.Text = "";
			this.txttieunhom.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txttieunhom_KeyDown);
			this.txttieunhom.Validated += new System.EventHandler(this.txttieunhom_Validated);
			this.txttieunhom.TextChanged += new System.EventHandler(this.txttieunhom_TextChanged);
			this.txttieunhom.Enter += new System.EventHandler(this.txttieunhom_Enter);
			// 
			// listtieunhom
			// 
			this.listtieunhom.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.listtieunhom.BackColor = System.Drawing.SystemColors.Info;
			this.listtieunhom.ColumnCount = 0;
			this.listtieunhom.Location = new System.Drawing.Point(256, 384);
			this.listtieunhom.MatchBufferTimeOut = 1000;
			this.listtieunhom.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
			this.listtieunhom.Name = "listtieunhom";
			this.listtieunhom.Size = new System.Drawing.Size(480, 17);
			this.listtieunhom.TabIndex = 158;
			this.listtieunhom.TextIndex = -1;
			this.listtieunhom.TextMember = null;
			this.listtieunhom.ValueIndex = -1;
			this.listtieunhom.Visible = false;
			// 
			// txttentieunhom
			// 
			this.txttentieunhom.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.txttentieunhom.Enabled = false;
			this.txttentieunhom.Location = new System.Drawing.Point(240, 384);
			this.txttentieunhom.Name = "txttentieunhom";
			this.txttentieunhom.Size = new System.Drawing.Size(544, 20);
			this.txttentieunhom.TabIndex = 157;
			this.txttentieunhom.Text = "";
			// 
			// txtmuc
			// 
			this.txtmuc.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.txtmuc.Enabled = false;
			this.txtmuc.Location = new System.Drawing.Point(104, 408);
			this.txtmuc.Name = "txtmuc";
			this.txtmuc.Size = new System.Drawing.Size(136, 20);
			this.txtmuc.TabIndex = 162;
			this.txtmuc.Text = "";
			this.txtmuc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtmuc_KeyDown);
			this.txtmuc.Validated += new System.EventHandler(this.txtmuc_Validated);
			this.txtmuc.TextChanged += new System.EventHandler(this.txtmuc_TextChanged);
			this.txtmuc.Enter += new System.EventHandler(this.txtmuc_Enter);
			// 
			// listmuc
			// 
			this.listmuc.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.listmuc.BackColor = System.Drawing.SystemColors.Info;
			this.listmuc.ColumnCount = 0;
			this.listmuc.Location = new System.Drawing.Point(256, 408);
			this.listmuc.MatchBufferTimeOut = 1000;
			this.listmuc.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
			this.listmuc.Name = "listmuc";
			this.listmuc.Size = new System.Drawing.Size(480, 17);
			this.listmuc.TabIndex = 161;
			this.listmuc.TextIndex = -1;
			this.listmuc.TextMember = null;
			this.listmuc.ValueIndex = -1;
			this.listmuc.Visible = false;
			// 
			// txttenmuc
			// 
			this.txttenmuc.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.txttenmuc.Enabled = false;
			this.txttenmuc.Location = new System.Drawing.Point(240, 408);
			this.txttenmuc.Name = "txttenmuc";
			this.txttenmuc.Size = new System.Drawing.Size(544, 20);
			this.txttenmuc.TabIndex = 160;
			this.txttenmuc.Text = "";
			// 
			// txttm
			// 
			this.txttm.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.txttm.Enabled = false;
			this.txttm.Location = new System.Drawing.Point(104, 432);
			this.txttm.Name = "txttm";
			this.txttm.Size = new System.Drawing.Size(136, 20);
			this.txttm.TabIndex = 165;
			this.txttm.Text = "";
			this.txttm.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txttm_KeyDown);
			this.txttm.Validated += new System.EventHandler(this.txttm_Validated);
			this.txttm.TextChanged += new System.EventHandler(this.txttm_TextChanged);
			this.txttm.Enter += new System.EventHandler(this.txttm_Enter);
			// 
			// listtm
			// 
			this.listtm.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.listtm.BackColor = System.Drawing.SystemColors.Info;
			this.listtm.ColumnCount = 0;
			this.listtm.Location = new System.Drawing.Point(256, 432);
			this.listtm.MatchBufferTimeOut = 1000;
			this.listtm.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
			this.listtm.Name = "listtm";
			this.listtm.Size = new System.Drawing.Size(480, 17);
			this.listtm.TabIndex = 164;
			this.listtm.TextIndex = -1;
			this.listtm.TextMember = null;
			this.listtm.ValueIndex = -1;
			this.listtm.Visible = false;
			// 
			// txttentm
			// 
			this.txttentm.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.txttentm.Enabled = false;
			this.txttentm.Location = new System.Drawing.Point(240, 432);
			this.txttentm.Name = "txttentm";
			this.txttentm.Size = new System.Drawing.Size(544, 20);
			this.txttentm.TabIndex = 163;
			this.txttentm.Text = "";
			// 
			// txttmct
			// 
			this.txttmct.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.txttmct.Enabled = false;
			this.txttmct.Location = new System.Drawing.Point(104, 456);
			this.txttmct.Name = "txttmct";
			this.txttmct.Size = new System.Drawing.Size(136, 20);
			this.txttmct.TabIndex = 168;
			this.txttmct.Text = "";
			this.txttmct.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txttmct_KeyDown);
			this.txttmct.Validated += new System.EventHandler(this.txttmct_Validated);
			this.txttmct.TextChanged += new System.EventHandler(this.txttmct_TextChanged);
			this.txttmct.Enter += new System.EventHandler(this.txttmct_Enter);
			// 
			// listtmct
			// 
			this.listtmct.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.listtmct.BackColor = System.Drawing.SystemColors.Info;
			this.listtmct.ColumnCount = 0;
			this.listtmct.Location = new System.Drawing.Point(256, 456);
			this.listtmct.MatchBufferTimeOut = 1000;
			this.listtmct.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
			this.listtmct.Name = "listtmct";
			this.listtmct.Size = new System.Drawing.Size(480, 17);
			this.listtmct.TabIndex = 167;
			this.listtmct.TextIndex = -1;
			this.listtmct.TextMember = null;
			this.listtmct.ValueIndex = -1;
			this.listtmct.Visible = false;
			// 
			// txttentmct
			// 
			this.txttentmct.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.txttentmct.Enabled = false;
			this.txttentmct.Location = new System.Drawing.Point(240, 456);
			this.txttentmct.Name = "txttentmct";
			this.txttentmct.Size = new System.Drawing.Size(544, 20);
			this.txttentmct.TabIndex = 166;
			this.txttentmct.Text = "";
			// 
			// txtnhmc
			// 
			this.txtnhmc.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.txtnhmc.Enabled = false;
			this.txtnhmc.Location = new System.Drawing.Point(104, 480);
			this.txtnhmc.Name = "txtnhmc";
			this.txtnhmc.Size = new System.Drawing.Size(136, 20);
			this.txtnhmc.TabIndex = 171;
			this.txtnhmc.Text = "";
			this.txtnhmc.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txtnhmc_KeyDown);
			this.txtnhmc.Validated += new System.EventHandler(this.txtnhmc_Validated_1);
			this.txtnhmc.TextChanged += new System.EventHandler(this.txtnhmc_TextChanged);
			// 
			// listnhmc
			// 
			this.listnhmc.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.listnhmc.BackColor = System.Drawing.SystemColors.Info;
			this.listnhmc.ColumnCount = 0;
			this.listnhmc.Location = new System.Drawing.Point(256, 480);
			this.listnhmc.MatchBufferTimeOut = 1000;
			this.listnhmc.MatchEntryStyle = AsYetUnnamed.MatchEntryStyle.FirstLetterInsensitive;
			this.listnhmc.Name = "listnhmc";
			this.listnhmc.Size = new System.Drawing.Size(480, 17);
			this.listnhmc.TabIndex = 170;
			this.listnhmc.TextIndex = -1;
			this.listnhmc.TextMember = null;
			this.listnhmc.ValueIndex = -1;
			this.listnhmc.Visible = false;
			// 
			// txttennhmc
			// 
			this.txttennhmc.Anchor = ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.txttennhmc.Enabled = false;
			this.txttennhmc.Location = new System.Drawing.Point(240, 480);
			this.txttennhmc.Name = "txttennhmc";
			this.txttennhmc.Size = new System.Drawing.Size(544, 20);
			this.txttennhmc.TabIndex = 169;
			this.txttennhmc.Text = "";
			// 
			// chkSudung
			// 
			this.chkSudung.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left);
			this.chkSudung.Location = new System.Drawing.Point(16, 537);
			this.chkSudung.Name = "chkSudung";
			this.chkSudung.TabIndex = 172;
			this.chkSudung.Text = "Sử dụng";
			// 
			// btn_in
			// 
			this.btn_in.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.btn_in.Image = ((System.Drawing.Bitmap)(resources.GetObject("btn_in.Image")));
			this.btn_in.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
			this.btn_in.Location = new System.Drawing.Point(560, 536);
			this.btn_in.Name = "btn_in";
			this.btn_in.Size = new System.Drawing.Size(60, 25);
			this.btn_in.TabIndex = 175;
			this.btn_in.Text = "      &In";
			this.btn_in.Click += new System.EventHandler(this.btn_in_Click);
			// 
			// frmDmmlns
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(792, 573);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.btn_in,
																		  this.chkSudung,
																		  this.txtnhmc,
																		  this.listnhmc,
																		  this.txttennhmc,
																		  this.txttmct,
																		  this.listtmct,
																		  this.txttentmct,
																		  this.txttm,
																		  this.listtm,
																		  this.txttentm,
																		  this.txtmuc,
																		  this.listmuc,
																		  this.txttenmuc,
																		  this.txttieunhom,
																		  this.listtieunhom,
																		  this.txttentieunhom,
																		  this.txtnhom,
																		  this.listnhom,
																		  this.txttennhom,
																		  this.label7,
																		  this.label6,
																		  this.label5,
																		  this.label4,
																		  this.label3,
																		  this.label2,
																		  this.cboLoaicapnhat,
																		  this.label1,
																		  this.butHuy,
																		  this.butBoqua,
																		  this.butLuu,
																		  this.butSua,
																		  this.butMoi,
																		  this.butKetthuc,
																		  this.dataGrid1,
																		  this.tim});
			this.Name = "frmDmmlns";
			this.Text = "Danh mục Mục lục ngân sách";
			this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
			this.Load += new System.EventHandler(this.frmDmmlns_Load);
			((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void frmDmmlns_Load(object sender, System.EventArgs e)
		{
			cboLoaicapnhat.SelectedIndex=0;
			//load list nhom			
			dsnhom=a.get_DMNhom();
			if(dsnhom!=null)
				dtnh=dsnhom.Tables[0];
			listnhom.DisplayMember="N";
			listnhom.ValueMember="TTK";
			listnhom.TextMember="N";
			listnhom.DataSource=dtnh;			
			//load list nhom muc chi			
			dsnhmuc=a.get_DMNhomMuc();
			if(dsnhmuc!=null)
				dtnhmc=dsnhmuc.Tables[0];
			listnhmc.DisplayMember="nhom";
			listnhmc.ValueMember="tennhom";
			listnhmc.TextMember="tennhom";
			listnhmc.DataSource=dtnhmc;

			this.currentRowBackBrush = new SolidBrush(Color.FromArgb(0,255, 255));
			this.disabledTextBrush = new SolidBrush(Color.FromArgb(255,0,0));
			this.currentRowFont = new Font(this.dataGrid1.Font.Name, this.dataGrid1.Font.Size, FontStyle.Regular);				

			load_grid();
			AddGridTableStyle();
			ref_text();
		}
		private void ena_object(bool ena)
		{	cboLoaicapnhat.Enabled=txtnhmc.Enabled=txttennhmc.Enabled=chkSudung.Enabled=ena;
			txtnhom.Enabled=txttennhom.Enabled=ena;
			txttieunhom.Enabled=txttentieunhom.Enabled=txtmuc.Enabled=txttenmuc.Enabled=ena;
			txttm.Enabled=txttentm.Enabled=txttmct.Enabled=txttentmct.Enabled=ena;
			butHuy.Enabled=butLuu.Enabled=butBoqua.Enabled=ena;
			butSua.Enabled=butHuy.Enabled=butKetthuc.Enabled=!ena;			
		}
		private void SetCellFormat(object sender, DataGridFormatCellEventArgs e)
		{
			try
			{
				bool discontinued = (bool) ((e.Column != 0) ? this.dataGrid1[e.Row, 0] : e.CurrentCellValue);
				if(e.Column > 0 && (bool)(this.dataGrid1[e.Row, 0]))//discontinued)
				{
					e.ForeBrush = this.disabledTextBrush;
				}
				else if(e.Column > 0 && e.Row == this.dataGrid1.CurrentRowIndex)
				{
					e.BackBrush = this.currentRowBackBrush;
					e.TextFont = this.currentRowFont;
				}
				if (decimal.Parse(dataGrid1[e.Row,3].ToString())!=0) e.ForeBrush=new SolidBrush(Color.Orange);
			}
			catch{}
		}
		private void RefreshRow(int row)
		{
			Rectangle rect = this.dataGrid1.GetCellBounds(row, 0);
			rect = new Rectangle(rect.Right, rect.Top, this.dataGrid1.Width, rect.Height);
			this.dataGrid1.Invalidate(rect);
		}
		private void BoolValueChanged(object sender, BoolValueChangedEventArgs e)
		{
			this.dataGrid1.EndEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row, false);
			RefreshRow(e.Row);
			this.dataGrid1.BeginEdit(this.dataGrid1.TableStyles[0].GridColumnStyles["Discontinued"],e.Row);
		}
		private void RefreshChildren(string text)
		{
			try
			{
				CurrencyManager cm= (CurrencyManager)BindingContext[dataGrid1.DataSource];
				DataView dv=(DataView)cm.List;
				dv.RowFilter="mtm like '%"+text.Trim()+"%' or stk like '%"+text.Trim()+"%'";
				ref_text();
			}
			catch{}
		}
		private void AddGridTableStyle()
		{
			DataGridTableStyle ts =new DataGridTableStyle();			
			ts.MappingName = ds.Tables[0].TableName;
			ts.AlternatingBackColor = Color.Beige;
			ts.BackColor = Color.GhostWhite;
			ts.ForeColor = Color.MidnightBlue;
			ts.GridLineColor = Color.RoyalBlue;
			ts.HeaderBackColor = Color.MidnightBlue;
			ts.HeaderForeColor = Color.Lavender;
			ts.SelectionBackColor = Color.Teal;
			ts.SelectionForeColor = Color.PaleGreen;
			ts.ReadOnly=false;
			ts.RowHeaderWidth=15;		
					
			DataGridTextBoxColumn TextCol=new DataGridTextBoxColumn();			
			TextCol.MappingName = "id";
			TextCol.HeaderText = "";
			TextCol.Width = 0;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "p";
			TextCol.HeaderText = "Phần";
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "n";
			TextCol.HeaderText = "Nhóm";
			TextCol.NullText="";
			TextCol.Width = 50;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);
			

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "tn";
			TextCol.HeaderText = "Tiểu nhóm";
			TextCol.Width = 70;
			TextCol.NullText="";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "stk";
			TextCol.HeaderText = "Số tk";
			TextCol.Width = 80;
			TextCol.NullText="";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ttk";
			TextCol.HeaderText = "Tên tk";
			TextCol.Width = 250;
			TextCol.NullText="";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName ="mtm";
			TextCol.HeaderText = "Mục tiểu mục";
			TextCol.NullText="";
			TextCol.Width = 100;
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "cc";
			TextCol.HeaderText = "CC";
			TextCol.Width = 150;
			TextCol.NullText="";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "cd";
			TextCol.HeaderText = "CD";
			TextCol.Width = 30;
			TextCol.NullText="";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "sp";
			TextCol.HeaderText = "SP";
			TextCol.Width = 30;
			TextCol.NullText="";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "nhmuc";
			TextCol.HeaderText = "Nhóm mục";
			TextCol.Width = 60;
			TextCol.NullText="";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "userid";
			TextCol.HeaderText = "Userid";
			TextCol.Width = 60;
			TextCol.NullText="";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);

			TextCol=new DataGridTextBoxColumn();
			TextCol.MappingName = "ngayud";
			TextCol.HeaderText = "Ngày ứng dụng";
			TextCol.Width = 100;
			TextCol.NullText="";
			ts.GridColumnStyles.Add(TextCol);
			dataGrid1.TableStyles.Add(ts);	
		}

		private void load_grid()
		{
			try
			{
				string sql="";
				sql="select ch.id as id,p,n,tn,stk,mtm,cc,ttk,cd,sp,ch.nhmuc as nhmuc,nh.nhom as nhom,nh.tennhom as tennhom,ch.sudung as sudung,ch.userid as userid,ch.ngayud as ngayud";
				sql+=" from "+a.user+".a_dmch ch left join "+a.user+".a_dmnhom nh on ch.nhmuc=nh.id where ch.sudung=1 order by cc";
				ds=a.get_data(sql);
				dataGrid1.DataSource=ds.Tables[0];
			}
			catch{};

		}

		private void load_dmTN(string s_nhom)
		{			
			dstn=a.get_DMTN(s_nhom);
			if(dstn!=null)
				dttn=dstn.Tables[0];
			listtieunhom.DisplayMember="TN";
			listtieunhom.ValueMember="TTK";
			listtieunhom.TextMember="TN";
			listtieunhom.DataSource=dttn;
		}
		private void load_dmMuc(string s_nhom,string s_tn)
		{			
			dsmuc=a.get_DMMuc(s_nhom,s_tn);
			if(dsmuc!=null)
				dtmuc=dsmuc.Tables[0];
			listmuc.DisplayMember="MUC";
			listmuc.ValueMember="TTK";
			listmuc.TextMember="MUC";
			listmuc.DataSource=dtmuc;
		}
		private void load_DMTM(string s_nhom,string s_tn,string s_muc)
		{		
			dstm=a.get_DMTMuc(s_nhom,s_tn,s_muc);
			if(dstm!=null)
				dttm=dstm.Tables[0];
			listtm.DisplayMember="TM";
			listtm.ValueMember="TTK";
			listtm.TextMember="TM";
			listtm.DataSource=dttm;
		}
		private void load_DMTMCT(string s_nhom,string s_tn,string s_muc,string s_tm)
		{			
			dstmct=a.get_DMTMCT(s_nhom,s_tn,s_muc,s_tm);
			if(dstmct!=null)
				dttmct=dstmct.Tables[0];			
			listtmct.DisplayMember="TMCT";
			listtmct.ValueMember="TTK";
			listtmct.TextMember="TMCT";
			listtmct.DataSource=dttmct;
			
		}		
		private void hidelist()
		{
			if(!listmuc.Focused) listmuc.Hide();
			if(!listnhmc.Focused) listnhmc.Hide();
			if(!listnhom.Focused) listnhom.Hide();
			if(!listtieunhom.Focused) listtieunhom.Hide();
			if(!listtm.Focused) listtm.Hide();
			if(!listtmct.Focused) listtmct.Hide();
		}

		private void Filter_List(LibList.List listDulieu,string s_Dkloc,string s_Tenfield_1,string s_Tenfield_2)
		{
			try
			{
				string s_Dieukien=s_Tenfield_1.Trim()+" like '%"+s_Dkloc.Trim()+"%'";
				if (s_Tenfield_2.Trim()!="") s_Dieukien+=" or "+s_Tenfield_2.Trim()+" like '%"+s_Dkloc.Trim()+"%'";
				CurrencyManager cm= (CurrencyManager)BindingContext[listDulieu.DataSource];
				System.Data.DataView dv=(System.Data.DataView)cm.List;
				dv.RowFilter=s_Dieukien;
			}
			catch{}
		}
		private void txtnhom_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listnhom.Focus();
			else if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
			{
				if (listnhom.Visible)	listnhom.Focus();
				else SendKeys.Send("{Tab}");
			}		
			else if (e.KeyCode==Keys.Escape) listnhom.Hide();
		}

		private void txtnhom_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==txtnhom)
			{
				Filter_List(listnhom,txtnhom.Text.Trim(),"N","");
				listnhom.BrowseToDmtk(txtnhom,txttennhom,txttieunhom,txtnhom.Location.X,txtnhom.Location.Y+txtnhom.Height,txtnhom.Width+txttennhom.Width+2,txtnhom.Height,100);
			}
		}

		private void txtnhom_Validated(object sender, System.EventArgs e)
		{
			DataRow r=a.getrowbyid(dtnh,"N='"+txtnhom.Text+"'");
			txtnhom.Text=(r!=null)?r["N"].ToString():"";		
			hidelist();
			
			txttennhom.Text="";
			if (txttennhom.Text!="")
			{				
				r=a.getrowbyid(dtnh,"N='"+txtnhom.Text.Trim()+"'");
				if (r!=null) 
				{
					txttennhom.Text=r["TTK"].ToString();					
					txttieunhom.Text=""; txttentieunhom.Text=""; b_tn=false;					
				}	
			}
		}

		private void txttieunhom_Enter(object sender, System.EventArgs e)
		{
			if (txtnhom.Text!="")
			{
				load_dmTN(txtnhom.Text);
				b_tn=true;
			}
			else
			{
				b_tn=false;
				txttieunhom.Text=""; txttentieunhom.Text="";
				SendKeys.Send("{Tab}");
			}
		}

		private void txttieunhom_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(dstn!=null && b_tn)
			{
				if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listtieunhom.Focus();
				else if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
				{
					if (listtieunhom.Visible)	listtieunhom.Focus();
					else SendKeys.Send("{Tab}");
				}		
				else if (e.KeyCode==Keys.Escape) listtieunhom.Hide();
			}
			else if(b_them)SendKeys.Send("{Tab}");
		}

		private void txttieunhom_TextChanged(object sender, System.EventArgs e)
		{
			if ((this.ActiveControl==txttieunhom && dstn!=null && b_tn && b_them)|| (this.ActiveControl==txttieunhom && dstn!=null && cboLoaicapnhat.Text!="Tiểu nhóm"))
			{
				Filter_List(listtieunhom,txttieunhom.Text.Trim(),"TN","");
				listtieunhom.BrowseToDmtk(txttieunhom,txttentieunhom,txtmuc,txttieunhom.Location.X,txttieunhom.Location.Y+txttieunhom.Height,txttieunhom.Width+txttentieunhom.Width+2,txttieunhom.Height,100);
			}
		}

		private void txttieunhom_Validated(object sender, System.EventArgs e)
		{
			if(b_them || cboLoaicapnhat.Text!="Tiểu nhóm")
			{
				DataRow r=a.getrowbyid(dttn,"TN='"+txttieunhom.Text+"'");
				txttieunhom.Text=(r!=null)?r["TN"].ToString():"";		
				hidelist();
				if (txttentieunhom.Text!="" && dstn!=null && b_tn)
				{				
					r=a.getrowbyid(dttn,"TN='"+txttieunhom.Text.Trim()+"'");
					if (r!=null) 
					{
						txttentieunhom.Text=r["TTK"].ToString();
						txtmuc.Text="";txttenmuc.Text="";b_muc=false;
					}	
				}
			}
		}

		private void txtmuc_Enter(object sender, System.EventArgs e)
		{
			if (txtnhom.Text!="" && txttieunhom.Text!="")
			{
				load_dmMuc(txtnhom.Text,txttieunhom.Text);
				b_muc=true;
			}
			else
			{
				b_muc=false;
				txtmuc.Text=""; txttenmuc.Text="";
				SendKeys.Send("{Tab}");
			}
		}

		private void txtmuc_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(dsmuc!=null && b_muc)
			{
				if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listmuc.Focus();
				else if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
				{
					if (listmuc.Visible)	listmuc.Focus();
					else SendKeys.Send("{Tab}");
				}		
				else if (e.KeyCode==Keys.Escape) listmuc.Hide();
			}
			else if(b_them)SendKeys.Send("{Tab}");
		}

		private void txtmuc_TextChanged(object sender, System.EventArgs e)
		{
			//(this.ActiveControl==txttieunhom && dstn!=null && b_tn && b_them)|| (this.ActiveControl==txttieunhom && dstn!=null && cboLoaicapnhat.Text!="Tiểu nhóm")
			if ((this.ActiveControl==txtmuc && dsmuc!=null && b_muc && b_them)||(this.ActiveControl==txtmuc && dsmuc!=null&&cboLoaicapnhat.Text!="Mục"))
			{
				Filter_List(listmuc,txtmuc.Text.Trim(),"MUC","");
				listmuc.BrowseToDmtk(txtmuc,txttenmuc,txttm,txtmuc.Location.X,txtmuc.Location.Y+txtmuc.Height,txtmuc.Width+txttenmuc.Width+2,txtmuc.Height,100);
			}
		}

		private void txtmuc_Validated(object sender, System.EventArgs e)
		{
			if(b_them||cboLoaicapnhat.Text!="Mục")
			{
				DataRow r=a.getrowbyid(dttn,"MUC='"+txtmuc.Text+"'");
				txtmuc.Text=(r!=null)?r["MUC"].ToString():"";		
				hidelist();
			
				txttenmuc.Text="";
				if (txttenmuc.Text!="" && dsmuc!=null && b_muc)
				{				
					r=a.getrowbyid(dtmuc,"MUC='"+txtmuc.Text.Trim()+"'");
					if (r!=null) 
					{
						txttenmuc.Text=r["TTK"].ToString();
						txttm.Text="";txttentm.Text="";b_tm=false;
					}	
				}
			}
		}

		private void txttm_Enter(object sender, System.EventArgs e)
		{
			if (txtnhom.Text!="" && txttieunhom.Text!="" && txtmuc.Text!="")
			{
				load_DMTM(txtnhom.Text,txttieunhom.Text,txtmuc.Text);
				b_tm=true;
			}
			else
			{
				b_tm=false;
				txttm.Text=""; txttentm.Text="";
				SendKeys.Send("{Tab}");
			}
		}

		private void txttm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(dstm!=null&&b_tm)
			{
				if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listtm.Focus();
				else if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
				{
					if (listtm.Visible)	listtm.Focus();
					else SendKeys.Send("{Tab}");
				}		
				else if (e.KeyCode==Keys.Escape) listtm.Hide();
			}
			else if(b_them)SendKeys.Send("{Tab}");
		}

		private void txttm_TextChanged(object sender, System.EventArgs e)
		{
			if ((this.ActiveControl==txttm && dstm!=null && b_tm && b_them) ||(this.ActiveControl==txttm && dstm!=null&&cboLoaicapnhat.Text!="Tiểu mục"))
			{
				Filter_List(listtm,txttm.Text.Trim(),"TM","");
				listtm.BrowseToDmtk(txttm,txttentm,txttmct,txttm.Location.X,txttm.Location.Y+txttm.Height,txttm.Width+txttentm.Width+2,txttm.Height,100);
			}
		}

		private void txttm_Validated(object sender, System.EventArgs e)
		{
			if(b_them||cboLoaicapnhat.Text!="Tiểu mục")
			{
				DataRow r=a.getrowbyid(dttm,"TM='"+txttm.Text+"'");
				txttm.Text=(r!=null)?r["TM"].ToString():"";		
				hidelist();
			
				txttentm.Text="";
				if (txttentm.Text!="" && dstm!=null && b_tm)
				{				
					r=a.getrowbyid(dttm,"TM='"+txttm.Text.Trim()+"'");
					if (r!=null) 
					{
						txttentm.Text=r["TTK"].ToString();
						txttmct.Text="";txttentmct.Text="";b_tmct=false;
					}	
				}
			}
		}

		private void txttmct_Enter(object sender, System.EventArgs e)
		{
			if (txtnhom.Text!="" && txttieunhom.Text!="" && txtmuc.Text!="" && txttm.Text!="")
			{
				load_DMTMCT(txtnhom.Text,txttieunhom.Text,txtmuc.Text,txttm.Text);
				b_tmct=true;
			}
			else
			{
				b_tmct=false;
				txttmct.Text=""; txttentmct.Text="";
				SendKeys.Send("{Tab}");
			}
		}

		private void txttmct_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(dstmct!=null&&b_tmct)
			{
				if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listtmct.Focus();
				else if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
				{
					if (listtmct.Visible)	listtmct.Focus();
					else SendKeys.Send("{Tab}");
				}		
				else if (e.KeyCode==Keys.Escape) listtmct.Hide();
			}
			else if(b_them)SendKeys.Send("{Tab}");
		}

		private void txttmct_TextChanged(object sender, System.EventArgs e)
		{
			if ((this.ActiveControl==txttmct && dstmct!=null && b_tmct && b_them) || (this.ActiveControl==txttmct && dstmct!=null&&cboLoaicapnhat.Text!="Tiểu mục chi tiết"))
			{
				Filter_List(listtmct,txttmct.Text.Trim(),"TMCT","");
				listtmct.BrowseToDmtk(txttmct,txttentmct,txtnhmc,txttmct.Location.X,txttmct.Location.Y+txttmct.Height,txttmct.Width+txttentmct.Width+2,txttmct.Height,100);
			}
		}

		private void txttmct_Validated(object sender, System.EventArgs e)
		{
			if(b_them|| cboLoaicapnhat.Text!="Tiểu mục chi tiết")
			{
				DataRow r=a.getrowbyid(dttmct,"TMCT"+txttmct.Text+"'");
				txttmct.Text=(r!=null)?r["TMCT"].ToString():"";		
				hidelist();
				txttentmct.Text="";
				if (txttentmct.Text!="" && dstmct!=null && b_tmct)
				{				
					r=a.getrowbyid(dttmct,"TMCT='"+txttmct.Text.Trim()+"'");
					if (r!=null) 
					{
						txttentmct.Text=r["TTK"].ToString();
					}	
				}
			}
		}

		private void txtnhmc_Validated(object sender, System.EventArgs e)
		{
			DataRow r=a.getrowbyid(dtnhmc,"NHOM='"+txtnhom.Text+"'");
			txtnhmc.Text=(r!=null)?r["NHOM"].ToString():"";		
			hidelist();			
			txttennhmc.Text="";
			if (txttennhmc.Text!="")
			{				
				r=a.getrowbyid(dtnhmc,"NHOM='"+txtnhmc.Text.Trim()+"'");
				if (r!=null) 
				{
					txttennhmc.Text=r["TENNHOM"].ToString();	
				}	
			}
		}

		private void txtnhmc_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if(e.KeyCode==Keys.Down || e.KeyCode==Keys.Up) listnhmc.Focus();
			else if (e.KeyCode==Keys.Enter || e.KeyCode==Keys.Tab)
			{
				if (listnhmc.Visible)	listnhmc.Focus();
				else SendKeys.Send("{Tab}");
			}		
			else if (e.KeyCode==Keys.Escape) listnhmc.Hide();
		}

		private void txtnhmc_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==txtnhmc)
			{
				Filter_List(listnhmc,txtnhmc.Text.Trim(),"N","");
				listnhmc.BrowseToDmtk(txtnhmc,txttennhmc,butMoi,txtnhmc.Location.X,txtnhmc.Location.Y+txtnhmc.Height,txtnhmc.Width+txttennhmc.Width+2,txtnhmc.Height,100);
			}
		}

		private void txtnhmc_Validated_1(object sender, System.EventArgs e)
		{
			try
			{
				DataRow r=a.getrowbyid(dtnhmc,"NHOM='"+txtnhmc.Text+"'");
				if(r!=null)
				{
					txtnhmc.Text=r["NHOM"].ToString();
					txttennhmc.Text=r["TENNHOM"].ToString();
				}
			}
			catch{};

//			DataRow r=a.getrowbyid(dtnhmc,"NHOM='"+txtnhmc.Text+"'");
//			txtnhmc.Text=(r!=null)?r["NHOM"].ToString():"";	
//			txttennhmc.Text=(r!=null)?r["TENNHOM"].ToString():"";
//			hidelist();
//			
//			
//			if (txttennhmc.Text!="")
//			{				
//				r=a.getrowbyid(dtnhmc,"NHOM='"+txtnhmc.Text.Trim()+"'");
//				if (r!=null) 
//				{
//					txttennhmc.Text=r["TENNHOM"].ToString();										
//				}	
//			}
		}
		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void butMoi_Click(object sender, System.EventArgs e)
		{
			b_them=false;	
			l_id=0;	
			ena_object(true);
			switch(cboLoaicapnhat.Text)
			{
				case "Tiểu nhóm":
					txtnhom.Enabled=txttennhom.Enabled=txttieunhom.Enabled=txttentieunhom.Enabled=true;
					txtmuc.Enabled=false;txttenmuc.Enabled=false;
					txttm.Enabled=false;txttentm.Enabled=false;
					txttmct.Enabled=false;txttentmct.Enabled=false;
					txttieunhom.Focus();					
					txttieunhom.Text="";txttentieunhom.Text="";
					txtmuc.Text="";txttenmuc.Text="";
					txttm.Text="";txttentm.Text="";
					txttmct.Text="";txttentmct.Text="";
					if(txttennhom.Text=="")
					{
						MessageBox.Show("Thêm Tiểu nhóm, phải khai báo Nhóm!","Thông báo!");
						txtnhom.Focus();
					}
					else if(txttennhmc.Text=="")
					{
						MessageBox.Show("Thêm Tiểu nhóm, phải khai báo Nhóm mục chi!","Thông báo!");
						txtnhmc.Focus();
					}
					else txttieunhom.Focus();
					break;
				case "Mục":	
					txtnhom.Enabled=txttennhom.Enabled=txttieunhom.Enabled=txttentieunhom.Enabled=true;
					txtmuc.Enabled=true;txttenmuc.Enabled=true;				
					txttm.Enabled=false;txttentm.Enabled=false;
					txttmct.Enabled=false;txttentmct.Enabled=false;
					txtmuc.Focus();
					txtmuc.Text="";txttenmuc.Text="";
					txttm.Text="";txttentm.Text="";
					txttmct.Text="";txttentmct.Text="";
					if(txttennhom.Text=="")
					{
						MessageBox.Show("Thêm Mục, phải khai báo Nhóm!","Thông báo!");
						txtnhom.Focus();
					}
					else if(txttentieunhom.Text=="")
					{
						MessageBox.Show("Thêm Mục, phải khai báo Tiểu nhóm!","Thông báo!");
						txttieunhom.Focus();
					}
					else if(txttennhmc.Text=="")
					{
						MessageBox.Show("Thêm Mục, phải khai báo Nhóm mục chi!","Thông báo!");
						txtnhmc.Focus();
					}
					else txtmuc.Focus();
					break;
				case "Tiểu mục":
					txtnhom.Enabled=txttennhom.Enabled=txttieunhom.Enabled=txttentieunhom.Enabled=true;
					txtmuc.Enabled=true;txttenmuc.Enabled=true;				
					txttm.Enabled=true;txttentm.Enabled=true;
					txttmct.Enabled=false;txttentmct.Enabled=false;
					txttm.Focus();
					txttm.Text="";txttentm.Text="";
					txttmct.Text="";txttentmct.Text="";
					if(txttennhom.Text=="")
					{
						MessageBox.Show("Thêm Tiểu mục, phải khai báo Nhóm!","Thông báo!");
						txtnhom.Focus();
					}
					else if(txttentieunhom.Text=="")
					{
						MessageBox.Show("Thêm Tiểu mục, phải khai báo Tiểu nhóm!","Thông báo!");
						txttieunhom.Focus();
					}				
					else if(txttenmuc.Text=="")
					{
						MessageBox.Show("Thêm Tiểu mục, phải khai báo Mục!","Thông báo!");
						txtmuc.Focus();
					}
					else if(txttennhmc.Text=="")
					{
						MessageBox.Show("Thêm Tiểu mục, phải khai báo Nhóm mục chi!","Thông báo!");
						txtnhmc.Focus();
					}
					else txttm.Focus();
					break;
				case "Tiểu mục chi tiết":
					txtnhom.Enabled=txttennhom.Enabled=txttieunhom.Enabled=txttentieunhom.Enabled=true;
					txtmuc.Enabled=true;txttenmuc.Enabled=true;				
					txttm.Enabled=true;txttentm.Enabled=true;
					txttmct.Enabled=true;txttentmct.Enabled=true;
					txttmct.Focus();
					txttmct.Text="";txttentmct.Text="";
					if(txttennhom.Text=="")
					{
						MessageBox.Show("Thêm Tiểu mục chi tiết, phải khai báo Nhóm!","Thông báo!");
						txtnhom.Focus();
					}
					else if(txttentieunhom.Text=="")
					{
						MessageBox.Show("Thêm Tiểu mục chi tiết, phải khai báo Tiểu nhóm!","Thông báo!");
						txttieunhom.Focus();
					}				
					else if(txttenmuc.Text=="")
					{
						MessageBox.Show("Thêm Tiểu mục chi tiết, phải khai báo Mục!","Thông báo!");
						txtmuc.Focus();
					}
					else if(txttentm.Text=="")
					{
						MessageBox.Show("Thêm Tiểu mục chi tiết, phải khai báo Tiểu mục!","Thông báo!");
						txttm.Focus();
					}
					else if(txttennhmc.Text=="")
					{
						MessageBox.Show("Thêm Tiểu mục chi tiết, phải khai báo Nhóm mục chi!","Thông báo!");
						txtnhmc.Focus();
					}
					else txttmct.Focus();
					break;
			}
		}

		private bool kiemtra()
		{
			//kiem tra trung ma	
			switch(cboLoaicapnhat.Text)
			{
				case "Tiểu nhóm":
					s_cc="C"+txtnhom.Text+txttieunhom.Text;
					break;
				case "Mục":
					s_cc="C"+txtnhom.Text+txttieunhom.Text+txtmuc.Text;
					break;
				case "Tiểu mục":
					s_cc="C"+txtnhom.Text+txttieunhom.Text+txtmuc.Text+txttm.Text;
					break;
				case "Tiểu mục chi tiết":
					s_cc="C"+txtnhom.Text+txttieunhom.Text+txtmuc.Text+txttm.Text+txttmct.Text;
					break;
			}				
			string sql="select * from "+a.user+".a_dmch where cc like '"+s_cc+"'and id<>"+dataGrid1[dataGrid1.CurrentRowIndex,0];
			if(a.get_data(sql).Tables[0].Rows.Count!=0)
			{
				MessageBox.Show("Trùng mã không cho lưu!",a.Msg);
				return false;
			}	
			switch(cboLoaicapnhat.Text)
			{
				case "Tiểu nhóm":
					if(txttieunhom.Text=="" ||txttentieunhom.Text=="")
					{
						MessageBox.Show("Nhập tiểu nhóm!");
						if(txttieunhom.Text=="")
							txttieunhom.Focus();
						else txttentieunhom.Focus();
						return false;
					}
					else if(txttieunhom.Text.Length!=4)
					{
						MessageBox.Show("Tiểu nhóm phải bằng 4 ký tự số!");
						txttieunhom.Focus();
						return false;
					}
					break;
				case "Mục":
					if(txtmuc.Text==""||txttenmuc.Text=="")
					{
						MessageBox.Show("Nhập mục!");
						if(txtmuc.Text=="")
							txtmuc.Focus();
						else txttenmuc.Focus();
						return false;
					}
					else if(txtmuc.Text.Length!=4)
					{
						MessageBox.Show("Mục phải bằng 4 ký tự số!");
						txtmuc.Focus();
						return false;
					}
					break;				
				case "Tiểu mục":
					if(txttm.Text==""||txttentm.Text=="")
					{
						MessageBox.Show("Nhập tiểu mục!");
						if(txttm.Text=="")
							txttm.Focus();
						else txttentm.Focus();
						return false;
					}
					else if(txttm.Text.Length!=4)
					{
						MessageBox.Show("Tiểu mục phải bằng 4 ký tự số!");
						txttm.Focus();
						return false;
					}
					break;
				case "Tiểu mục chi tiết":
					if(txttmct.Text==""||txttentmct.Text=="")
					{
						MessageBox.Show("Nhập tiểu mục chi tiết!");
						if(txttmct.Text=="")
							txttmct.Focus();
						else txttentmct.Focus();
						return false;
					}
					else if(txttmct.Text.Length!=2)
					{
						MessageBox.Show("Tiểu mục chi tiết phải bằng 2 ký tự số!");
						txttmct.Focus();
						return false;
					}
					break;					
			}
			return true;
		}
		private void butLuu_Click(object sender, System.EventArgs e)
		{	
			if (!kiemtra()) return ;			
			if (l_id==0)
			{
				try
				{
					l_id=long.Parse(a.get_data("select max(id) from "+a.user+".a_dmch").Tables[0].Rows[0][0].ToString())+1;
				}
				catch{l_id=1;}
			}	
			switch(cboLoaicapnhat.Text)
			{
				case "Tiểu nhóm":
					i_sp=3;i_cd=4;
					s_ttk=txttentieunhom.Text;
					s_cc="C"+txtnhom.Text+txttieunhom.Text;
					break;
				case "Mục":
					i_sp=4;i_cd=3;
					s_stk=txtmuc.Text;
					s_ttk=txttenmuc.Text;
					s_mtm=txtmuc.Text;
					s_cc="C"+txtnhom.Text+txttieunhom.Text+txtmuc.Text;
					break;
				case "Tiểu mục":
					i_sp=5;i_cd=2;
					s_stk=txttm.Text;
					s_ttk=txttentm.Text;
					s_mtm=txtmuc.Text+"."+txttm.Text;
					s_cc="C"+txtnhom.Text+txttieunhom.Text+txtmuc.Text+txttm.Text;
					break;
				case "Tiểu mục chi tiết":
					i_sp=6;i_cd=1;
					s_stk=txttm.Text+"."+txttmct.Text;
					s_ttk=txttentmct.Text;
					s_mtm=txtmuc.Text+"."+txttm.Text+"."+txttmct.Text;
					s_cc="C"+txtnhom.Text+txttieunhom.Text+txtmuc.Text+txttm.Text+txttmct.Text;
					break;
			}
			if (txttennhmc.Text.Trim()!="") i_nhmuc=int.Parse(a.getrowbyid(dtnhmc,"nhom='"+txtnhmc.Text.Trim()+"'")["id"].ToString().Trim());
			if (!a.upd_dmmlns(l_id,"C",txtnhom.Text,txttieunhom.Text,s_stk,s_cc,s_ttk,i_cd,i_sp,i_nhmuc,chkSudung.Checked?1:0,0,i_userid,s_mtm,DateTime.Now))
			{
				MessageBox.Show("Không cập nhật thông tin "+this.Text.Trim()+" !",a.Msg);
				return;
			}
			load_grid();
			ref_text();
			ena_object(false);		
			b_them=true;
		}
		private DataSet get_cc(string s_subcc)
		{
			bool flag_dsnull=true;
			string sql="";
			DataSet ds_get=new DataSet();
			if(s_subcc!="") sql="select ttk from "+a.user+".a_dmch where cc='"+s_subcc+"'";						
			try
			{
				ds_get=a.get_data(sql);

				if(ds_get!=null && ds_get.Tables[0].Rows.Count!=0)
					flag_dsnull=true;
			}
			catch{flag_dsnull=false;};
			if(flag_dsnull) return ds_get;
			else return null;
		}
		private void ref_text()
		{			
			l_id=long.Parse(dataGrid1[dataGrid1.CurrentRowIndex,0].ToString() );
			DataRow r =a.getrowbyid(ds.Tables[0],"id="+l_id);
			if(r!=null)
			{	
				txtnhom.Text=r["n"].ToString().Trim();
				string s_subccnhom="",s_subcctn="",s_subccmuc="",s_subcctm="",s_subcctmct="";
				DataSet ds_get = new DataSet();
				string s_tdnhom="",s_tdtn="",s_tdmuc="",s_tdtm="",s_tdtmct="";
				//lay ten nhom
				s_subccnhom=r["cc"].ToString().Trim().Length>=5?r["cc"].ToString().Trim().Substring(0,5):"";
				ds_get=get_cc(s_subccnhom);
				if(ds_get!=null && ds_get.Tables[0].Rows.Count!=0)
					s_tdnhom=ds_get.Tables[0].Rows[0][0].ToString();				
				//lay ten tieu nhom
				s_subcctn=r["cc"].ToString().Trim().Length>=9?r["cc"].ToString().Trim().Substring(0,9):"";
				ds_get=get_cc(s_subcctn);
				if(ds_get!=null && ds_get.Tables[0].Rows.Count!=0)
					s_tdtn=ds_get.Tables[0].Rows[0][0].ToString();
				//lay ten muc
				s_subccmuc=r["cc"].ToString().Trim().Length>=13?r["cc"].ToString().Trim().Substring(0,13):"";						
				ds_get=get_cc(s_subccmuc);
				if(ds_get!=null && ds_get.Tables[0].Rows.Count!=0)
					s_tdmuc=ds_get.Tables[0].Rows[0][0].ToString();
				//lay ten tieu muc
				s_subcctm=r["cc"].ToString().Trim().Length>=17?r["cc"].ToString().Trim().Substring(0,17):"";						
				ds_get=get_cc(s_subcctm);
				if(ds_get!=null && ds_get.Tables[0].Rows.Count!=0)
					s_tdtm=ds_get.Tables[0].Rows[0][0].ToString();
				//lay ten tieu muc chi tiet
				s_subcctmct=r["cc"].ToString().Trim().Length>=19?r["cc"].ToString().Trim().Substring(0,19):"";						
				ds_get=get_cc(s_subcctmct);
				if(ds_get!=null && ds_get.Tables[0].Rows.Count!=0)
					s_tdtmct=ds_get.Tables[0].Rows[0][0].ToString();			
				txttennhom.Text=s_tdnhom;
				txttieunhom.Text=r["tn"].ToString().Trim();
				txttentieunhom.Text=txttieunhom.Text!=""?s_tdtn:"";
				txtmuc.Text=(r["mtm"].ToString().ToString().Trim()!="" && r["mtm"].ToString().Trim().Length>=4)?r["mtm"].ToString().ToString().Trim().Substring(0,4):"";
				txttenmuc.Text=txtmuc.Text!=""?s_tdmuc:"";
				txttm.Text=(r["mtm"].ToString().Trim()!="" && r["mtm"].ToString().Trim().Length>=8)?r["mtm"].ToString().Trim().Substring(5,4):"";
				txttentm.Text=txttm.Text!=""?s_tdtm:"";
				txttmct.Text=(r["mtm"].ToString().Trim()!="" && r["mtm"].ToString().Trim().Length>=12)?r["mtm"].ToString().Trim().Substring(10,2):"";
				txttentmct.Text=txttmct.Text!=""?s_tdtmct:"";
				txtnhmc.Text=r["nhom"].ToString().Trim();
				txttennhmc.Text=r["tennhom"].ToString().Trim();
				chkSudung.Checked=r["sudung"].ToString().Trim()=="1"?true:false;
				cboLoaicapnhat.Text=r["cd"].ToString().Trim()=="4"?"Tiểu nhóm":r["cd"].ToString().Trim()=="3"?"Mục":r["cd"].ToString().Trim()=="2"?"Tiểu mục":"Tiểu mục chi tiết";
			}			
		}

		private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
		{
			if(b_them)
				ref_text();
		}

		private void butBoqua_Click(object sender, System.EventArgs e)
		{
			b_them=true;
			ref_text();
			ena_object(false);
		}

		private void butHuy_Click(object sender, System.EventArgs e)
		{
			if (ds.Tables[0].Rows.Count==1)
			{
				MessageBox.Show("Không cho phép hủy ?",a.Msg);
				return;
			}	
			try
			{
				if (a.get_data("select * from "+a.user+".a_sd"+s_mmyy.Substring(2)+" where mtmuc="+long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString())).Tables[0].Rows.Count!=0)
				{
					MessageBox.Show("Nội dung đang sử dụng không cho phép hủy !",a.Msg);
					return;
				}
				//kiem tra doi tuong muon xoa da "lá" chua?
				int i_id=int.Parse(dataGrid1[dataGrid1.CurrentRowIndex,0].ToString() );
				DataRow r =a.getrowbyid(ds.Tables[0],"id="+i_id);
				if(r!=null)
				{						
					string s_subcc=r["cc"].ToString().Trim();
					string sql="select * from "+a.user+".a_dmch where cc like '"+s_subcc+"%' and id<>"+i_id+"";
					if(a.get_data(sql).Tables[0].Rows.Count!=0)
					{
						MessageBox.Show("Còn mục con không cho phép huỷ!",a.Msg);
						return;
					}					
				}
				
			}
			catch{}
			if (MessageBox.Show("Đồng ý hủy thông tin này !",a.Msg,MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
			{
				a.execute_data("delete from "+a.user+".a_dmch where id="+long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString()));
				load_grid();
			}
		}

		private void butSua_Click(object sender, System.EventArgs e)
		{
			b_them=false;			
			if (ds.Tables[0].Rows.Count==0) return;
			l_id=long.Parse(dataGrid1[dataGrid1.CurrentCell.RowNumber,0].ToString());
			ena_object(true);
			//cboLoaicapnhat.Enabled=false;
			switch(cboLoaicapnhat.Text)
			{
				case "Tiểu nhóm":
					txttieunhom.Focus();
					break;
				case "Mục":
					txtmuc.Focus();
					break;
				case "Tiểu mục":
					txttm.Focus();
					break;
				case "Tiểu mục chi tiết":
					txttmct.Focus();
					break;
			}
		}

		private void tim_Enter(object sender, System.EventArgs e)
		{
			tim.Text="";
		}

		private void tim_TextChanged(object sender, System.EventArgs e)
		{
			if (this.ActiveControl==tim) RefreshChildren(tim.Text);
		}

		private void cboLoaicapnhat_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			if(!b_them)
			{
				switch(cboLoaicapnhat.Text)
				{
					case "Tiểu nhóm":
						txtnhom.Enabled=txttennhom.Enabled=txttieunhom.Enabled=txttentieunhom.Enabled=true;
						txtmuc.Enabled=false;txttenmuc.Enabled=false;
						txttm.Enabled=false;txttentm.Enabled=false;
						txttmct.Enabled=false;txttentmct.Enabled=false;
						txttieunhom.Focus();					
						txttieunhom.Text="";txttentieunhom.Text="";
						txtmuc.Text="";txttenmuc.Text="";
						txttm.Text="";txttentm.Text="";
						txttmct.Text="";txttentmct.Text="";					
						break;
					case "Mục":	
						txtnhom.Enabled=txttennhom.Enabled=txttieunhom.Enabled=txttentieunhom.Enabled=true;
						txtmuc.Enabled=true;txttenmuc.Enabled=true;				
						txttm.Enabled=false;txttentm.Enabled=false;
						txttmct.Enabled=false;txttentmct.Enabled=false;
						txtmuc.Focus();
						txtmuc.Text="";txttenmuc.Text="";
						txttm.Text="";txttentm.Text="";
						txttmct.Text="";txttentmct.Text="";					
						break;
					case "Tiểu mục":
						txtnhom.Enabled=txttennhom.Enabled=txttieunhom.Enabled=txttentieunhom.Enabled=true;
						txtmuc.Enabled=true;txttenmuc.Enabled=true;				
						txttm.Enabled=true;txttentm.Enabled=true;
						txttmct.Enabled=false;txttentmct.Enabled=false;
						txttm.Focus();
						txttm.Text="";txttentm.Text="";
						txttmct.Text="";txttentmct.Text="";					
						break;
					case "Tiểu mục chi tiết":
						txtnhom.Enabled=txttennhom.Enabled=txttieunhom.Enabled=txttentieunhom.Enabled=true;
						txtmuc.Enabled=true;txttenmuc.Enabled=true;				
						txttm.Enabled=true;txttentm.Enabled=true;
						txttmct.Enabled=true;txttentmct.Enabled=true;
						txttmct.Focus();
						txttmct.Text="";txttentmct.Text="";					
						break;
				}
			}
		}

		private void btn_in_Click(object sender, System.EventArgs e)
		{
		
			DataSet _DataSet =new DataSet();
			_DataSet=a._rdanhmuctieumuc(tim.Text,a.user);
			frmReport _frmReport =new frmReport(a,_DataSet,"11","KT_rmuctieumuc.rpt");
			_frmReport.ShowDialog();
		}
	}
}
